#include <iostream>

__global__ void vectorAdd(int *a, int *b, int *c, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) {
        c[i] = a[i] + b[i];
    }
}

extern "C" {
    void launchCudaProgram(int *a, int *b, int *c, int n) {
        int *d_a, *d_b, *d_c;
        cudaMalloc(&d_a, n * sizeof(int));
        cudaMalloc(&d_b, n * sizeof(int));
        cudaMalloc(&d_c, n * sizeof(int));

        cudaMemcpy(d_a, a, n * sizeof(int), cudaMemcpyHostToDevice);
        cudaMemcpy(d_b, b, n * sizeof(int), cudaMemcpyHostToDevice);

        vectorAdd<<<(n + 255) / 256, 256>>>(d_a, d_b, d_c, n);

        cudaMemcpy(c, d_c, n * sizeof(int), cudaMemcpyDeviceToHost);

        cudaFree(d_a);
        cudaFree(d_b);
        cudaFree(d_c);
    }
}
